﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web.Mvc;
using ControleRepresentante.Common;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Statistics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebSite.Controllers;

namespace ControleRepresentante.UnitTests
{
    public static class TestScenarionUsuarioHelper
    {
        public static void DeleteUser(this TestScenario testScenario, Guid userId)
        {
            IUserRepository userRepository = testScenario.RepositoryProvider.GetRepository<IUserRepository>();
            StatisticsBase statistics;
            int countBefore = userRepository.GetModels(out statistics).Count();
            using (UsuarioController usuarioController = new UsuarioController(testScenario.RepositoryProvider, testScenario.AreaOperacaoRegistry, testScenario.SessionConfiguration, testScenario.ControllerExtensionProvider))
            {
                usuarioController.Delete(userId);
                usuarioController.Delete(userId, null);
                Assert.AreEqual(countBefore - 1, userRepository.GetModels(out statistics).Count());
            }
        }

        public static Guid CreateUser(this TestScenario testScenario, string username, string password, string email, bool ativo)
        {
            IUserRepository userRepository = testScenario.RepositoryProvider.GetRepository<IUserRepository>();
            StatisticsBase statistics;
            int countBefore = userRepository.GetModels(out statistics).Count();
            using (UsuarioController usuarioController = new UsuarioController(testScenario.RepositoryProvider, testScenario.AreaOperacaoRegistry, testScenario.SessionConfiguration, testScenario.ControllerExtensionProvider))
            {
                usuarioController.Create();
                NameValueCollection nameValueCollection = new NameValueCollection();
                foreach (Area area in AreaOperacaoRegistry.GetAreas())
                {
                    foreach (Operacao operacao in testScenario.AreaOperacaoRegistry.GetOperacoes(area))
                    {
                        nameValueCollection.Add(area + "_" + operacao, true.ToString());
                    }
                }
                FormCollection formCollection = new FormCollection(nameValueCollection);
                ActionResult actionResult = usuarioController.Create(username, password, email, ativo, null, null, null, null, formCollection);

                IEnumerable<User> users = userRepository.GetModels(out statistics);
                Assert.AreEqual(countBefore + 1, users.Count());
                User addedUser = users.FirstOrDefault(x => x.Username == username);

                Assert.AreEqual(username, addedUser.Username);
                Assert.AreEqual(password.Encrypt(), addedUser.Password);
                Assert.AreEqual(ativo, addedUser.Ativo);
                Assert.IsFalse(addedUser.IsAdmin);
                Assert.AreEqual(1, testScenario.GetCurrentRepresentante().Users.Count(x => x.Username == username));
                Assert.IsInstanceOfType(actionResult, typeof(RedirectToRouteResult));
                RedirectToRouteResult redirectToRouteResult = (RedirectToRouteResult)actionResult;
                Assert.AreEqual("Details", redirectToRouteResult.RouteValues["action"]);
                Assert.AreEqual(addedUser.Id, redirectToRouteResult.RouteValues["id"]);
                return addedUser.Id;
            }
        }
    }
}